<?php
class Staff extends Person
{	
	/*
	Determines if a given person_id is a exist
	*/
	function exists($person_id)
	{
		$this->db->from('staffs');	
		$this->db->join('people', 'people.person_id = staffs.person_id');
		$this->db->where('staffs.person_id',$person_id);
		$query = $this->db->get();
		
		return ($query->num_rows()==1);
	}
	
	/*
	Determines if a given person_id is a exist
	*/
	function payroll_exists($item_id)
	{
		$this->db->from('staff_payroll');	
		$this->db->where('payroll_id',$item_id);
		$query = $this->db->get();
		return ($query->num_rows()==1);
	}
	
	/*
	Returns all the staff
	*/
	function get_all()
	{
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');			
		$this->db->order_by("last_name", "asc");
		return $this->db->get();		
	}
	
	/*
	Perform a search on staff
	*/
	function search($search)
	{
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');		
		$this->db->like('first_name', $search);
		$this->db->or_like('last_name', $search); 
		$this->db->or_like('email', $search); 
		$this->db->or_like('phone_number', $search);
		$this->db->or_like('account_number', $search);
		$this->db->or_like('bank_account_number', $search);
		$this->db->or_like('position', $search);
		$this->db->or_like('social_security_no', $search);		
		$this->db->or_like("CONCAT(`first_name`,' ',`last_name`)",$search);
		$this->db->order_by("last_name", "asc");
		
		return $this->db->get();	
	}
	
	/*
	Gets information about a particular staff
	*/
	function get_info($staff_id)
	{
		$this->db->from('staffs');	
		$this->db->join('people', 'people.person_id = staffs.person_id');
		$this->db->where('staffs.person_id',$staff_id);
		$query = $this->db->get();
		
		if($query->num_rows()==1)
		{
			return $query->row();
		}
		else
		{
			//Get empty base parent object, as $staff_id is NOT an staff
			$person_obj=parent::get_info(-1);
			
			//Get all the fields from staff table
			$fields = $this->db->list_fields('staffs');
			
			//append those fields to base parent object, we we have a complete empty object
			foreach ($fields as $field)
			{
				$person_obj->$field='';
			}
			
			return $person_obj;
		}
	}
	
	/*
	Gets information about multiple staff
	*/
	function get_multiple_info($staff_ids)
	{
		$this->db->from('staffs');
		$this->db->join('people', 'people.person_id = staffs.person_id');		
		$this->db->where_in('staffs.person_id',$staff_ids);
		$this->db->order_by("last_name", "asc");
		return $this->db->get();		
	}
	
	/*
	Inserts or updates a staff
	*/
	function save(&$person_data, &$staff_data,$staff_id=false)
	{
		$success=false;
		//Run these queries as a transaction, we want to make sure we do all or nothing
		$this->db->trans_start();
		
		if(parent::save($person_data,$staff_id))
		{
			if (!$staff_id or !$this->exists($staff_id))
			{
				$staff_data['person_id'] = $person_data['person_id'];
				$success = $this->db->insert('staffs',$staff_data);				
			}
			else
			{
				$this->db->where('person_id', $staff_id);
				$success = $this->db->update('staffs',$staff_data);
			}
			
		}
		
		$this->db->trans_complete();		
		return $success;
	}
	
	/*
	Deletes one staff
	*/
	function delete($staff_id)
	{
		$success=false;
		
		//Run these queries as a transaction, we want to make sure we do all or nothing
		$this->db->trans_start();
		
		//delete from customers table
		if($this->db->delete('staffs', array('person_id' => $staff_id)))
		{
			//delete from Person table
			$success = parent::delete($staff_id);
		}
		
		$this->db->trans_complete();		
		return $success;
	}
	
	/*
	Deletes a list of staff
	*/
	function delete_list($staff_ids)
	{
		$success=false;

		//Run these queries as a transaction, we want to make sure we do all or nothing
		$this->db->trans_start();

		$this->db->where_in('person_id',$staff_ids);
		if ($this->db->delete('staffs'))
		{
			$success = parent::delete_list($staff_ids);
		}
		
		$this->db->trans_complete();		
		return $success;
 	}
 	
 	/*
	Get search suggestions to find staff
	*/
	function get_search_suggestions($search,$limit=25)
	{
		$suggestions = array();
		
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');	
		$this->db->like('first_name', $search); 
		$this->db->or_like('last_name', $search);
		$this->db->or_like("CONCAT(`first_name`,' ',`last_name`)",$search);		
		$this->db->order_by("last_name", "asc");		
		$by_name = $this->db->get();
		foreach($by_name->result() as $row)
		{
			$suggestions[]=$row->first_name.' '.$row->last_name;		
		}
		
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');	
		$this->db->like("email",$search);
		$this->db->order_by("email", "asc");		
		$by_email = $this->db->get();
		foreach($by_email->result() as $row)
		{
			$suggestions[]=$row->email;		
		}

		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');	
		$this->db->like("phone_number",$search);
		$this->db->order_by("phone_number", "asc");		
		$by_phone = $this->db->get();
		foreach($by_phone->result() as $row)
		{
			$suggestions[]=$row->phone_number;		
		}
		
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');	
		$this->db->like("account_number",$search);
		$this->db->order_by("account_number", "asc");		
		$by_account_number = $this->db->get();
		foreach($by_account_number->result() as $row)
		{
			$suggestions[]=$row->account_number;		
		}
		
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');	
		$this->db->like("bank_account_number",$search);
		$this->db->order_by("bank_account_number", "asc");		
		$by_bank_account_number = $this->db->get();
		foreach($by_bank_account_number->result() as $row)
		{
			$suggestions[]=$row->bank_account_number;		
		}
		
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');	
		$this->db->like("position",$search);
		$this->db->order_by("position", "asc");		
		$by_position = $this->db->get();
		foreach($by_position->result() as $row)
		{
			$suggestions[]=$row->position;		
		}
		
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');	
		$this->db->like("social_security_no",$search);
		$this->db->order_by("social_security_no", "asc");		
		$by_social_security_no = $this->db->get();
		foreach($by_social_security_no->result() as $row)
		{
			$suggestions[]=$row->social_security_no;		
		}
		
		
		//only return $limit suggestions
		if(count($suggestions > $limit))
		{
			$suggestions = array_slice($suggestions, 0,$limit);
		}
		return $suggestions;
	
	}
	
	/*
	Get search suggestions to find staff
	*/
	function get_staff_search_suggestions($search,$limit=25)
	{
		$suggestions = array();
		
		$this->db->from('staffs');
		$this->db->join('people','staffs.person_id=people.person_id');	
		$this->db->like('first_name', $search); 
		$this->db->or_like('last_name', $search);
		$this->db->or_like("CONCAT(`first_name`,' ',`last_name`)",$search);		
		$this->db->order_by("last_name", "asc");		
		$by_name = $this->db->get();
		foreach($by_name->result() as $row)
		{
			$suggestions[]=$row->person_id.'|'.$row->first_name.' '.$row->last_name;		
		}
		
		//only return $limit suggestions
		if(count($suggestions > $limit))
		{
			$suggestions = array_slice($suggestions, 0,$limit);
		}
		return $suggestions;

	}
	
	/*
	Returns all the payrolls for a staff
	*/
	function get_staff_payrolls($staff_id)
	{
		$this->db->from('staff_payroll');
		$this->db->where('person_id', $staff_id);
		$this->db->order_by("date", "asc");
		return $this->db->get();		
	}
	
	/*
	Returns the total allowaces for a payroll
	*/
	function get_allowance_totals($payroll_id)
	{
		$this->db->select('sum(amount) as payroll_total');
		$this->db->from('staff_allowances');	
		$this->db->where('payroll_id',$payroll_id);
		$this->db->group_by('payroll_id');
		return $this->db->get();
	}
	
	/*
	Returns the total non taxable allowaces for a payroll
	*/
	function get_non_taxable_allowance_totals($payroll_id)
	{
	  return $this->db->query("SELECT SUM(amount) as payroll_total FROM ".$this->db->dbprefix('staff_allowances')." WHERE payroll_id = '$payroll_id'  AND taxable = '0'  GROUP BY  payroll_id");
		
		//$this->db->select('sum(amount) as payroll_total');
		//$this->db->from('staff_allowances');	
		//$this->db->where('payroll_id = ',$payroll_id);
		//$this->db->group_by('payroll_id');
		//return $this->db->get();
	}
	
	/*
	Returns the total deductios for a payroll
	*/
	function get_deduction_totals($payroll_id)
	{
		$this->db->select('sum(amount) as payroll_total');
		$this->db->from('staff_deductions');	
		$this->db->where('payroll_id',$payroll_id);
		$this->db->group_by('payroll_id');
		return $this->db->get();
	}
	
	/*
	Inserts  a payroll
	*/
	function save_payroll(&$item_data)
	{		
		if($this->db->insert('staff_payroll',$item_data))
		{
			$item_data['payroll_id']=$this->db->insert_id();
			return true;
		}
		return false;
	}
	
	/*
	Update  a payroll
	*/
	function update_payroll(&$item_data,$item_id)
	{		
		if ($item_id or $this->payroll_exists($item_id))
		{
			if($this->db->where('payroll_id', $item_id))
			{
				return $this->db->update('staff_payroll',$item_data);
			}
			return false;
		}	
	}
	
	/*
	Inserts  a allowance
	*/
	function save_payroll_allowance(&$item_data)
	{		
		return $this->db->insert('staff_allowances',$item_data);	
	}

	function update_payroll_allowance($items_data,$update_id=false)
	{
		if($update_id)
		{
			$this->db->where('allowance_id', $update_id);
			return $this->db->update('staff_allowances',$items_data);
		}	
		return false;
	}
	
	/*
	Inserts  a staff_deductions
	*/
	function save_payroll_deductions(&$item_data)
	{		
		return $this->db->insert('staff_deductions',$item_data);	
	}
	
	function update_payroll_deductions($items_data,$update_id=false)
	{
		if($update_id)
		{
			$this->db->where('deductions_id', $update_id);
			return $this->db->update('staff_deductions',$items_data);
		}	
		return false;
	}
	
	//Delete Allowance
	function delete_allowance($item_ids)
	{
		$this->db->where_in('allowance_id',$item_ids);
		return $this->db->delete('staff_allowances');	
 	}
	
	//Delete Deductions
	function delete_deductions($item_ids)
	{
		$this->db->where_in('deductions_id',$item_ids);
		return $this->db->delete('staff_deductions');	
 	}
	
	
	/*
	Deletes a Payrolls
	*/
	function delete_payroll($item_ids)
	{
		$this->db->where_in('payroll_id',$item_ids);
		$this->db->delete('staff_allowances');
		
		$this->db->where_in('payroll_id',$item_ids);
		$this->db->delete('staff_deductions');
		
		$this->db->where_in('payroll_id',$item_ids);
		return $this->db->delete('staff_payroll');		
 	}
	
	
	/*
	Gets ssf for edting
	*/
	function get_payroll_for_editing($item_id)
	{
		$final_data = array();
		
		//Get the info about allowance
		$this->db->from('staff_allowances');
		$this->db->where('payroll_id',$item_id);
		$final_data['query_allowance']  = $this->db->get();
		
		//Get the info about deductions
		$this->db->from('staff_deductions');
		$this->db->where('payroll_id',$item_id);
		$final_data['query_deductions']  = $this->db->get();
		
		//$this->db->select('order_item_cost,order_quantity,order_item_number,order_number,order_item_id,name,category,brand');
		$this->db->from('staff_payroll');
		$this->db->where('payroll_id',$item_id);
		$final_data['query_payroll'] = $this->db->get();
			
		return $final_data;
	}
	
}
?>
